import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { DefaultConfig } from './common/define';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import * as xmlparser from 'express-xml-bodyparser';
import { Transport } from '@nestjs/microservices';
import { Logger } from '@nestjs/common';
import { HttpExceptionFilter } from './middleware/http.filter';
import { TransformHttpInterceptor } from './middleware/res.filter';

const options = new DocumentBuilder()
    .setTitle('微信公众号登陆服务')
    .setDescription('微信公众号登陆服务接口')
    .setVersion('1.0')
    .build();

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.connectMicroservice({
    transport: Transport.REDIS,
    options: {
      url: 'redis://localhost:6379',
    },
  });
  // 异常拦截
  app.useGlobalFilters(new HttpExceptionFilter());
  // response 包装
  // app.useGlobalInterceptors(new TransformHttpInterceptor());

  // 兼容xml
  app.use(xmlparser());
  app.enableCors();

  // swagger
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api-docs', app, document);

  await app.startAllMicroservicesAsync();
  await app.listen(DefaultConfig.HostPort);
}
bootstrap();
